/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-11
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-14
 * @Description: 自定义Listitle, 列表项
 */
import 'package:flutter/material.dart';
import 'c_square_avatar.dart';

class CListTile extends StatelessWidget {
  const CListTile({
    super.key,
    this.icon,
    this.iconColor,
    this.imageUrl,
    this.backgroundColor,
    required this.title,
    this.titleWidget,
    this.subtitle,
    this.trailing,
    this.onTap,
    this.onLongPress,
    this.selected = false,
    this.minTileHeight = 72,
    this.iconborderRadius,
    this.iconSize,
    this.leadingWidget,
  });

  // 左侧leading
  final IconData? icon; // 图标
  final Color? iconColor; // 图标颜色

  final String? imageUrl; // 头像图片地址

  final String? leadingWidget; // 头部组件

  final Color? backgroundColor; // 头像/图标背景颜色

  // 中间文字标题

  final String title; // 标题
  final String? subtitle; // 子标题

  final Widget? titleWidget;

  // 右侧
  final Widget? trailing;

  // 点击事件
  final VoidCallback? onTap;
  // 长按事件
  final VoidCallback? onLongPress;

  // 选中
  final bool selected;

  // 列表项高度
  final double? minTileHeight;

  final double? iconborderRadius;
  final double? iconSize;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: _buildLeading(),
      title: _buildTitle(),
      // 仅在有传入 subtitle 时才显示
      subtitle: subtitle != null
          ? Text(
              subtitle!,
              overflow: TextOverflow.ellipsis,
            )
          : null,

      // 右侧
      trailing: trailing,
      // 事件
      onTap: onTap,
      onLongPress: onLongPress,
      // 样式
      shape: RoundedRectangleBorder(),
      titleTextStyle: TextStyle(
          fontWeight: FontWeight.w500, fontSize: 20, color: Colors.black),
      subtitleTextStyle: TextStyle(color: Colors.grey),
      // 选中
      selected: selected,
      selectedTileColor: Color(0XFFDBE2F0),
      tileColor: Colors.white,
      // 最小高度
      minTileHeight: minTileHeight,
    );
  }

  _buildLeading() {
    if (leadingWidget != null) return leadingWidget;

    if (icon == null && imageUrl == null) {
      return null;
    } else {
      return SquareAvatar(
        icon: icon,
        iconColor: iconColor,
        imageSource: imageUrl,
        size: iconSize ?? 54,
        backgroundColor: backgroundColor,
        borderRadius: iconborderRadius ?? 6,
      );
    }
  }

  Widget? _buildTitle() {
    if (titleWidget != null) {
      return titleWidget;
    } else {
      return Text(
        title,
        overflow: TextOverflow.visible,
      );
    }
  }
}
